package com.meixiang.activity.homes.service;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.adapter.service.ShowPicturePagerAdapter;
import com.meixiang.entity.services.ServiceDetail;
import com.meixiang.main.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 查看图片
 */
public class ShowPictureActivity extends BaseActivity{

    @Bind(R.id.iv_title_left)
    ImageView mIvTitleLeft;
    @Bind(R.id.tv_title_content)
    TextView mTvTitleContent;
    @Bind(R.id.ll_title)
    RelativeLayout mLlTitle;
    @Bind(R.id.viewpager)
    ViewPager mViewpager;
    @Bind(R.id.tv_text_title)
    TextView mTvTextTitle;
    @Bind(R.id.tv_text_content)
    TextView mTvTextContent;
    @Bind(R.id.ll_text)
    LinearLayout mLlText;
    //private List<View> viewList;
    private ArrayList<ServiceDetail.StepListBean> mShowPictures; //传入服务对象，则显示底部文字
    private List<String> urls;//传入url集合，则只展示图片
    private ShowPicturePagerAdapter mAdapter;
    private Boolean step;//判断是否是服务步骤
    private int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_show_picture);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        urls=getIntent().getStringArrayListExtra("urls"); //url集合
        position=getIntent().getIntExtra("position", 0); //当前下标
        Log.e("position",position+"");
        mShowPictures=getIntent().getParcelableArrayListExtra("data");
         //服务步骤对象集合
        if (null!=urls){
            step=false;
            mLlText.setVisibility(View.GONE);
            mShowPictures=new ArrayList<>();
            for (int i=0;i<urls.size();i++){
                ServiceDetail.StepListBean picture=new ServiceDetail.StepListBean();
                picture.setStepImage(urls.get(i));
                mShowPictures.add(picture);
            }
        }else if (mShowPictures != null) {
            step = true;
        }else{
            new RuntimeException("请传入图片url集合或服务步骤对象集合");
            return;
        }
        mAdapter = new ShowPicturePagerAdapter(this);
        mAdapter.setImageViewOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (step) {
                    if (mLlTitle.getVisibility() == View.VISIBLE) {
                        mLlTitle.setVisibility(View.GONE);
                        mLlText.setVisibility(View.GONE);
                    } else {
                        mLlTitle.setVisibility(View.VISIBLE);
                        mLlText.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mLlTitle.getVisibility() == View.VISIBLE) {
                        mLlTitle.setVisibility(View.GONE);
                    } else {
                        mLlTitle.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
        mAdapter.setData(mShowPictures);
        mViewpager.setAdapter(mAdapter);
        mViewpager.setCurrentItem(position);
        mTvTitleContent.setText(mViewpager.getCurrentItem() + 1 + "/" + mShowPictures.size());
        mTvTextTitle.setText(mShowPictures.get(mViewpager.getCurrentItem()).getStepName());
        mTvTextContent.setText(mShowPictures.get(mViewpager.getCurrentItem()).getStepDesc());
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTvTitleContent.setText(position + 1 + "/" + mShowPictures.size());
                if (step) {
                    mTvTextTitle.setText(mShowPictures.get(position).getStepName());
                    mTvTextContent.setText(mShowPictures.get(position).getStepDesc());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }


    @OnClick({R.id.viewpager,R.id.iv_title_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.viewpager:
                if (step){
                    if (mLlTitle.getVisibility()==View.VISIBLE){
                        mLlTitle.setVisibility(View.GONE);
                        mLlText.setVisibility(View.GONE);
                    }else{
                        mLlTitle.setVisibility(View.VISIBLE);
                        mLlText.setVisibility(View.VISIBLE);
                    }
                }else{
                    if (mLlTitle.getVisibility()==View.VISIBLE){
                        mLlTitle.setVisibility(View.GONE);
                    }else{
                        mLlTitle.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }

    }

}
